<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class HongqunController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;


    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));

    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }


        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );


        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //原生支付宝H5
            case 3:
            case 4:
            case 17:
            case 18:
                $pay_bankcode = 'ali_h5_wake';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        // 构造要请求的参数数组
        $parameter = array(
            "assCode" => $this->config['mch_id'],
            "assPayOrderNo" =>$return['orderid'],
            "assPayMoney" => strval($return['amount']*100),
            "assNotifyUrl" => $return['notifyurl'],
            "assReturnUrl" => $return['callbackurl'],
            "assCancelUrl" =>$return['callbackurl'],
            "paymentType" => $pay_bankcode,
            "subPayCode" => $pay_bankcode,
        );


        ksort($parameter);
        $org = '&'.httpstr($parameter).$this->config['signkey'];

        $parameter["sign"] =  strtoupper(md5($org)) ;

        $res = curl($this->config['gateway'], httpstr($parameter));//curl_post

        $resp = json_decode($res,true);

        $code = $resp['payUrl'];

        if(!$code){
            if($resp['message']){
                $this->showmessage($resp['message'], ['code' => C("THIRDCODES." . $this->class)]);
            }

            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("Location: {$resp['payUrl']}");
    }



    //通知
    public function notifyurl(){
        //$this->debug_callback();

        $p = I('post.');

        /*
            array (
              'assCode' => '190505175912629',
              'sysPayOrderNo' => 'E1905051952406539',
              'assPayMoney' => '10000',
              'assPayMessage' => 'E1905051952406539',
              'succTime' => '20190506101138',
              'sign' => '8636F72CD8350EC02191D683CB7E364C',
              'respMsg' => '已付款',
              'assPayOrderNo' => '2019050519524474496',
              'respCode' => '1',
            )
         * */


        //获取订单信息
        $orderid = $p['assPayOrderNo'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['sysPayOrderNo'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();


        $sign = $p['sign'];
        unset($p['sign']);

        ksort($p);
        $org = '&'.httpstr($p).$this->config['signkey'];

        $sign1 =  strtoupper(md5($org)) ;


        if($sign1 !== $sign ){
            exit("sign_wrong");
        }

        if($p['respCode'] !== '1'){
            exit("pay_fail");
        }


        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}